/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PurchaseDetails2.java
 *
 * Created on Dec 14, 2009, 2:52:09 PM
 */

package GUI;

import Business.PurchaseProcess_BS;
import Business.Purchase_BS;
import Business.SupplierProcess_BS;
import Business.loadPurchaseDetails;
import DAO.Purchase_DAO;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author anhvtn_b00545
 */
public class PurchaseDetails extends javax.swing.JDialog {
    //private int pcId;
    SupplierProcess_BS supplierBO = new SupplierProcess_BS();
    private loadPurchaseDetails load;
    private Purchase_BS ps;

    /** Creates new form PurchaseDetails2 */
    public PurchaseDetails(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

//    public void setPurchaseId(int pcId) {
//        this.pcId = pcId;
//        //ps.setPCID(pcId);
//    }
    public void setPurchase(Purchase_BS purchase) {
         PurchaseProcess_BS pcp = PurchaseProcess_BS.getPurchase();
       this.ps = purchase;
       //System.out.println(pcId);
       try {
            load = pcp.viewPurchaseDetails(ps.getPCID());
            tblPurchaseDetails.setModel(load);
            //System.out.println(tblPurchaseDetails.getRowCount());
             float totalPrice=0;
            int row= tblPurchaseDetails.getRowCount();
            //System.out.println(row);
            for(int i=0; i<row; i++)
            {
                Float subPrice= (Float)tblPurchaseDetails.getValueAt(i,3);
                //System.out.println(tblPurchaseDetails.getValueAt(i, 0));
                totalPrice += subPrice;
            }
            ps.setTotal(totalPrice);
            lblTotal.setText(String.valueOf(totalPrice));
        } catch (Exception e) {
            e.printStackTrace();
        }
        lblDatePaid.setText(purchase.getPcDate());
        lblSupplierName.setText(purchase.getSName());
        txtPaid.setText(String.valueOf(purchase.getPaid()));
        txtUnpaid.setText(String.valueOf(purchase.getUnpaid()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btUpdate = new javax.swing.JButton();
        lblCode1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPurchaseDetails = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        lblPaid = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtBalance = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        txtPay = new javax.swing.JTextField();
        btPay = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtPaid = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtUnpaid = new javax.swing.JLabel();
        lblbDate = new javax.swing.JLabel();
        lblDatePaid = new javax.swing.JLabel();
        lblSupplierName = new javax.swing.JLabel();
        btClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Purchase Details", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Monotype Corsiva", 1, 36), new java.awt.Color(51, 102, 0))); // NOI18N

        btUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Edit.png"))); // NOI18N
        btUpdate.setText("Update");
        btUpdate.setActionCommand("buttton1");
        btUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUpdateActionPerformed(evt);
            }
        });

        lblCode1.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCode1.setText("Supplier ");

        tblPurchaseDetails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblPurchaseDetails.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblPurchaseDetailsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblPurchaseDetails);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Pay"));

        lblPaid.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPaid.setText("Pay : ");

        jLabel2.setText("$");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Balance : ");

        txtBalance.setText("0");

        jLabel4.setText("$");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Total : ");

        lblTotal.setText("0");

        txtPay.setText("0");

        btPay.setText("Pay");
        btPay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPayActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Paid : ");

        txtPaid.setText("0");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Unpaid: ");

        txtUnpaid.setText("0");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(lblPaid)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPay, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPaid, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                        .addGap(93, 93, 93))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(btPay)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtUnpaid, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBalance, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)))
                .addGap(72, 72, 72))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblTotal)
                    .addComponent(jLabel5)
                    .addComponent(txtPaid)
                    .addComponent(jLabel6)
                    .addComponent(txtUnpaid))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblPaid)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtPay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txtBalance)
                        .addComponent(jLabel4)
                        .addComponent(btPay))))
        );

        lblbDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblbDate.setText("Date Paid : ");

        lblDatePaid.setText("date");

        lblSupplierName.setForeground(new java.awt.Color(204, 0, 0));
        lblSupplierName.setText("jLabel1");

        btClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Close-icon.png"))); // NOI18N
        btClose.setText("Close");
        btClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblbDate)
                            .addComponent(lblCode1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDatePaid, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSupplierName, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(335, 335, 335))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(429, Short.MAX_VALUE)
                .addComponent(btUpdate)
                .addGap(18, 18, 18)
                .addComponent(btClose)
                .addGap(68, 68, 68))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblbDate)
                    .addComponent(lblDatePaid))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCode1)
                    .addComponent(lblSupplierName))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 305, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btUpdate)
                    .addComponent(btClose))
                .addGap(16, 16, 16))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUpdateActionPerformed
        // TODO add your handling code here:
        
        Purchase_DAO pd = new Purchase_DAO();
        //boolean flag = false;

        Date today = new Date();
        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        String pcDate = format.format(today.getTime());
        Float paid = Float.parseFloat(txtPaid.getText());
        Float unPaid = Float.parseFloat(txtUnpaid.getText());
        //ps = new Purchase_BS(pcId,paid,unPaid,pcDate);
        ps.setPaid(paid);
        ps.setUnpaid(unPaid);
        ps.setPcDate(pcDate);
        if(!pd.updatePurchase(ps)) {
            JOptionPane.showMessageDialog(this, "Cannot Update Purchase");
            return;
        } else {
            JOptionPane.showMessageDialog(this, "Update Successfull!");
        }
        this.dispose();
}//GEN-LAST:event_btUpdateActionPerformed

    private void tblPurchaseDetailsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblPurchaseDetailsMouseClicked

        if (evt.getClickCount() > 1) {
            int row = tblPurchaseDetails.getSelectedRow();
        }
}//GEN-LAST:event_tblPurchaseDetailsMouseClicked

    private void btPayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPayActionPerformed
        // TODO add your handling code here:
        Float paid = Float.valueOf(txtPaid.getText()).floatValue();
        float totalPrice= ps.getTotal();
        float unpaid= Float.valueOf(txtUnpaid.getText().trim()).floatValue();
        float pay = Float.valueOf(txtPay.getText().trim()).floatValue();
        float balance = Float.valueOf(txtBalance.getText().trim()).floatValue();

        if(pay !=0) {
            if(pay > unpaid ) {
                JOptionPane.showMessageDialog(this,"Paid Extra Money : " +(pay - unpaid)+ " $");
                paid =paid + unpaid;
                balance = 0;
                unpaid =0;
                ps.setPaid(paid);
                ps.setUnpaid(balance);
                txtPaid.setText(String.valueOf(ps.getPaid()));
                txtUnpaid.setText(String.valueOf(ps.getUnpaid()));
            } else{
                unpaid = unpaid - pay;
                balance = unpaid - pay;
                paid += pay ;
                ps.setPaid(paid);
                ps.setUnpaid(unpaid);
                txtPaid.setText(String.valueOf(ps.getPaid()));
                txtUnpaid.setText(String.valueOf(ps.getUnpaid()));
            }
        } else {
            balance= unpaid;
            ps.setPaid(paid);
            ps.setUnpaid(unpaid);
        }

        txtPaid.setText(String.valueOf(ps.getPaid()));
        txtUnpaid.setText(String.valueOf(ps.getUnpaid()));
        txtBalance.setText(String.valueOf(unpaid));
}//GEN-LAST:event_btPayActionPerformed

    private void btCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btCloseActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btClose;
    private javax.swing.JButton btPay;
    private javax.swing.JButton btUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCode1;
    private javax.swing.JLabel lblDatePaid;
    private javax.swing.JLabel lblPaid;
    private javax.swing.JLabel lblSupplierName;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JLabel lblbDate;
    private javax.swing.JTable tblPurchaseDetails;
    private javax.swing.JLabel txtBalance;
    private javax.swing.JLabel txtPaid;
    private javax.swing.JTextField txtPay;
    private javax.swing.JLabel txtUnpaid;
    // End of variables declaration//GEN-END:variables

}
